package com.example.zkweixiu.app.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.activity.ChangeNumber1Activity;
import com.example.zkweixiu.app.activity.ChangePassword1Activity;
import com.example.zkweixiu.app.activity.LoginActivity;
import com.example.zkweixiu.app.activity.NicknameActivity;
import com.example.zkweixiu.app.activity.SignActivity;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.bean.UserBean;
import com.example.zkweixiu.app.pro.PublicPro;
import com.bumptech.glide.Glide;
import com.example.zkweixiu.app.utils.SPUtils;
import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import de.hdodenhof.circleimageview.CircleImageView;

public class HomeFragment4 extends Fragment implements View.OnClickListener {

    private Intent intent;
    private TextView tv_login;
    private TextView tv_login11;
    private RelativeLayout rv_exit;
    private TextView tv_changesign;
    private LinearLayout ll_sign;
    private LinearLayout ll_nick;
    private LinearLayout ll_num;
    private LinearLayout ll_password;
    private View iv_changesign2;
    private TextView tv_signa;
    private CircleImageView iv_loginimg;


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    if (PublicPro.userBean != null) {

                        tv_login.setText(PublicPro.userBean.getData().getGuaName());
                        String guaPhone = PublicPro.userBean.getData().getGuaPhone();
                        StringBuilder sb = new StringBuilder(guaPhone);
                        sb.replace(3, 9, "******"); //隐藏号码中段
                        tv_login11.setText(sb.toString());

                        if (TextUtils.isEmpty(PublicPro.userBean.getData().getGuaSigna())) {
                            tv_signa.setText("暂未设置签名");

                        } else {
                            tv_signa.setText(PublicPro.userBean.getData().getGuaSigna());
                        }

                        if (TextUtils.isEmpty(PublicPro.userBean.getData().getGuaImg())) {
                            //  tv_signa.setText("暂未设置签名");

                        } else {
                            Glide.with(getActivity()).load(PublicPro.userBean.getData().getGuaImg()).into(iv_loginimg);

                        }

                    }
                    break;
            }

        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment4, container, false);

        return view;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tv_login = view.findViewById(R.id.tv_login);
        tv_login11 = view.findViewById(R.id.tv_login11);
        tv_signa = view.findViewById(R.id.tv_signa); //签名
        rv_exit = view.findViewById(R.id.rv_exit); //点击退出登录
        tv_changesign = view.findViewById(R.id.tv_changesign);
        iv_changesign2 = view.findViewById(R.id.iv_changesign2);
        ll_nick = view.findViewById(R.id.ll_nick);
        ll_sign = view.findViewById(R.id.ll_sign);
        ll_num = view.findViewById(R.id.ll_num);
        ll_password = view.findViewById(R.id.ll_password);
        iv_loginimg = view.findViewById(R.id.iv_loginimg); //头像

        tv_changesign.setOnClickListener(this);
        iv_changesign2.setOnClickListener(this);
        ll_nick.setOnClickListener(this);
        ll_sign.setOnClickListener(this);
        ll_num.setOnClickListener(this);
        ll_password.setOnClickListener(this);
        rv_exit.setOnClickListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onStart() {
        super.onStart();
        getUserData();

    }

    /**
     * 重新刷新用户信息
     */
    private void getUserData() {
        OkHttpUtils
                .get()
                .url(PublicPro.GET_DATA)
                .addParams("wx_uuid", PublicPro.userBean.getData().getGuaUuid())
                .addParams("token", PublicPro.userBean.getData().getGuaToken())
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);
                        if (response.contains("用户过期,请重新登录")) {

                            RxActivityTool.skipActivityAndFinishAll(getActivity(), LoginActivity.class);
                            return;

                        }

                        UserBean userBean = new Gson().fromJson(response, UserBean.class);

                        if (userBean.getStatus() == 200) {
                            // handler.sendEmptyMessage(200);

                            PublicPro.userBean = userBean;
                            SPUtils.put(getActivity(), "user", response);

                            // SPUtils.clear(context);
                            //PublicPro.userBean = null;
                            // RxActivityTool.skipActivityAndFinishAll(getActivity(), LoginActivity.class);
                            handler.sendEmptyMessage(200);
                        } else {
                            Toast.makeText(getActivity(), userBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_nick:
                intent = new Intent(getActivity(), NicknameActivity.class);  //昵称修改
                startActivity(intent);
                break;
            case R.id.ll_num:
                intent = new Intent(getActivity(), ChangeNumber1Activity.class);//账号修改
                startActivity(intent);
                break;
            case R.id.ll_password:
                intent = new Intent(getActivity(), ChangePassword1Activity.class);//密码修改
                startActivity(intent);
                break;
            case R.id.iv_changesign2:
            case R.id.ll_sign:
            case R.id.tv_changesign:
                intent = new Intent(getActivity(), SignActivity.class);//签名修改

                startActivity(intent);
                break;


            case R.id.rv_exit: //退出登录

                final RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(getActivity());
                rxDialogSureCancel.getTitleView().setBackgroundResource(R.mipmap.logoset2);
                rxDialogSureCancel.getContentView().setText("是否退出登录");
                rxDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //  getActivity().finish();

                        SPUtils.clear(getActivity());
                        RxActivityTool.skipActivityAndFinishAll(getActivity(), LoginActivity.class);
                        //  Toast.makeText(context,"退出成功", Toast.LENGTH_SHORT).show();
                    }
                });
                rxDialogSureCancel.show();
                break;
        }
    }
}
